<?php
/**
 * Created by PhpStorm.
 * User: Kevin
 * Date: 2019/1/15
 * Time: 13:50
 */
namespace app\shop\controller;
use think\Controller;
use think\Db;
class Login extends Controller
{
    /**
     * 登录页面
     * @return mixed
     */
    public function indexLogin(){
        if(session('?user_id')){
            $this->redirect(url('shop/index/index'));
        }
        $logo = Db::name('about')->where('id',5)->value('content');
        //-----
        $http = http_type();
        $code = uuid();
        $ercode = Qrcode("{$http}/api/Timing/qrcode?code={$code}");
        $insert = [
            'pc_code_url' => $ercode,
            'pc_code' => $code,
            'pc_time' => date('Y-m-d H:i:s')
        ];
        $res = Db::table('up_z_pcqr')->insert($insert);
        if($res){
            $this->assign([
                'logo' => $logo,
                'code' => $code,
                'ercode' => $ercode,
            ]);
            return $this->fetch('login/index_login');
        }else{
            exit('请刷新重试！');
        }
    }
    public function waitApp(){
        $code = $_POST['code'];
        $isHave = Db::table('up_z_pcqr')
            ->where('pc_code', $code)
            ->find();
        if($isHave){
            if($isHave['pc_user_id']){
                if($isHave['pc_status'] == 1){ //已扫
                    //入session
                    $user = Db::name('u_user')
                        ->where('user_id',$isHave['pc_user_id'])
                        ->field('user_id,user_username,user_image,user_phone,user_type,user_status')
                        ->find();
                    if($user['user_type'] != 1){
                        return json(['status' => 0, 'msg' => "身份错误！"]);
                    }
                    if($user['user_status'] == 2){
                        return json(['status' => 0, 'msg' => "账号已被冻结！"]);
                    }
                    if($user['user_status'] == 3){
                        return json(['status' => 0, 'msg' => "账号已注销！"]);
                    }
                    session('user_id',$user['user_id']);//id
                    session('user_username',$user['user_username']);//名称
                    session('user_image',$user['user_image']);//头像
                    session('user_phone',$user['user_phone']);//手机号
                    //告知前端
                    $http = http_type();
                    return json(['status' => 200, 'msg' => "/shop/Index/index"]);
                }else{ //超时了
                    return json(['status' => 100, 'msg' => '']);
                }
            }else{
                $cha = time() - strtotime($isHave['pc_time']);
                if($cha > 300){ //超时了
                    return json(['status' => 100, 'msg' => '']);
                }else{ //未超时还没扫
                    return json(['status' => 199, 'msg' => '']);
                }
            }
        }else{
            return json(['status' => 0, 'msg' => '此码有误，请刷新页面！']);
        }
    }

    /*
     * 退出登录
     */
    public function loginOut(){
        session(null);
        $this->redirect(url('shop/login/indexLogin'));
    }
}